package com.tianda.rbac_demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.tianda.rbac_demo.domain.BaseResult;
import com.tianda.rbac_demo.domain.MenuVO;
import com.tianda.rbac_demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 管理员控制
 */
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 查询菜单
     * @return
     */
    @GetMapping("/admin/menu")
    public BaseResult getMenu() {
        List<MenuVO> menus = adminService.getMenuByUsername("zhangsan");
        return BaseResult.success(menus);
    }

    /**
     * 登录
     * @param username 账号
     * @param password 密码
     * @return
     */
    @PostMapping("/admin/doLogin")
    public BaseResult doLogin(String username, String password) {
        return adminService.login(username,password);
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/admin/logout")
    public BaseResult logout() {
        StpUtil.logout();
        return BaseResult.success();
    }
}
